<template>
  <div class="buyer-registration-switch">
    <el-card>
      <el-form ref="ruleForm" :model="ruleForm" label-width="150px" class="demo-ruleForm">
        <el-form-item label="是否开放买手注册：">
          <el-radio v-model="ruleForm.configValue" :label="false">关闭</el-radio>
          <el-radio v-model="ruleForm.configValue" :label="true">开启</el-radio>
          <el-button type="primary" size="mini" :loading="isLoading" @click="buyerRegistration">修改</el-button>
        </el-form-item>
        <el-form-item label="新手教学H5地址：">
          <el-input v-model="h5Form.configValue" style="width: 500px" />
          <el-button type="primary" size="mini" :loading="isLoading" @click="updateH5Url">修改</el-button>
        </el-form-item>
        <el-form-item label="客服H5地址：">
          <el-input v-model="h5CustomerForm.configValue" style="width: 500px" />
          <el-button type="primary" size="mini" :loading="isCustomerLoading" @click="updateCustomerH5Url">修改</el-button>
        </el-form-item>
        <el-form-item label="客户端版本管理：">
          <div>
            <label style="width: 95px;display: inline-block">ios版本：</label>
            <el-input v-model="clientForm.ios" style="width: 500px;margin-top: 10px;" />
          </div>
          <div>
            
            <label style="width: 95px;display: inline-block">android版本：</label>
            <el-input v-model="clientForm.android" style="width: 500px;margin-top: 10px;" />
          </div>
          <div>
            
            <label style="width: 95px;display: inline-block">下载地址：</label>
            <el-input v-model="clientForm.download" style="width: 500px;margin-top: 10px;" />
          </div>
          <el-button type="primary" size="mini" :loading="isClientLoading" @click="updateClient">修改</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import { 
  getBuyerRegisterAllowed,
  updateBuyerRegisterAllowed,
  getNoviceTeaching,
  updateNoviceTeaching,
  getCustomerService,
  updateCustomerService,
  getAppVersion,
  updateAppVersion } from '@/api/user'
export default {
  data() {
    return {
      ruleForm: {},
      h5Form: {},
      h5CustomerForm: {},
      clientForm: {},
      isLoading: false,
      isCustomerLoading: false,
      isClientLoading: false
    }
  },
  async mounted() {
    await this.getBuyerRegisterAllowed()
    await this.getNoviceTeaching()
    await this.getCustomerService()
    await this.getAppVersion()
  },
  methods: {
    getBuyerRegisterAllowed() {
      getBuyerRegisterAllowed().then(res => {
        this.$set(this.ruleForm, 'configValue', res)
      })
    },
    getNoviceTeaching() {
      getNoviceTeaching().then(res => {
        this.$set(this.h5Form, 'configValue', res)
      })
    },
    getAppVersion() {
      getAppVersion().then(res => {
        this.clientForm = res
      })
    },
    updateClient() {
      this.isClientLoading = true
      updateAppVersion(this.clientForm).then(res => {
        this.isClientLoading = false
        this.$message({
          message: `修改成功！`,
          type: 'success'
        })
      }).catch(() => {
        this.isClientLoading = false
      })
    },
    buyerRegistration() {
      this.isLoading = true
      updateBuyerRegisterAllowed(this.ruleForm).then(res => {
        this.isLoading = false
        this.$message({
          message: `已${this.ruleForm.configValue ? '开启' : '关闭'}买手注册`,
          type: 'success'
        })
      }).catch(() => {
        this.isLoading = false
      })
    },
    updateH5Url() {
      this.isLoading = true
      updateNoviceTeaching(this.h5Form).then(res => {
        this.isLoading = false
        this.$message({
          message: `修改成功！`,
          type: 'success'
        })
      }).catch(() => {
        this.isLoading = false
      })
    },
    getCustomerService() {
      this.isCustomerLoading = true
      getCustomerService().then(res => {
        this.$set(this.h5CustomerForm, 'configValue', res)
        this.isCustomerLoading = false
      }).catch(() => {
        this.isCustomerLoading = false
      })
    },
    updateCustomerH5Url() {
      this.isCustomerLoading = true
      updateCustomerService(this.h5CustomerForm).then(res => {
        this.$message({
          message: `修改成功！`,
          type: 'success'
        })
        this.isCustomerLoading = false
      }).catch(() => {
        this.isCustomerLoading = false
      })
    }
  }
}
</script>
<style lang="scss">
  .buyer-registration-switch{

  }
</style>
